package 回溯法;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class 电话号码的数字组合 {
    static List<String> list= new ArrayList<>();
    static StringBuffer stringBuffer = new StringBuffer();
    public static void main(String[] args) {
        String digits = "2";
        letterCombinations(digits);
        System.out.println(list);

    }
    static public List<String> letterCombinations(String digits) {
        list.clear();
        Map<Character,String> mapping = new HashMap<>();
        mapping.put('2',"abc");
        mapping.put('3',"def");
        mapping.put('4',"ghi");
        mapping.put('5',"jkl");
        mapping.put('6',"mno");
        mapping.put('7',"pqrs");
        mapping.put('8',"tuv");
        mapping.put('9',"wxyz");
        if(digits.length() == 0)
            return list;
        else
            fun(mapping,digits,0);
        return list;
    }
    static public void fun(Map<Character,String> mapping, String digits, int n){
        if(n == digits.length()){
            list.add(stringBuffer.toString());
            return;
        }
        String str = mapping.get(digits.charAt(n));
        for (int j = 0; j < str.length(); j++) {
            stringBuffer.append(str.charAt(j));
            fun(mapping,digits,n+1);
            stringBuffer.deleteCharAt(stringBuffer.length()-1);
        }
    }
}
